/*
 * InvokeDialogTest.java
 * 
 * Written by Joseph Bowbeer and released to the public domain,
 * as explained at http://creativecommons.org/licenses/publicdomain
 */

package jozart.invokedialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import jozart.swingutils.InvokeUtils;
import jozart.swingutils.SwingWorker;

/**
 * Tests the {@link InvokeUtils#invokeConfirmDialog InvokeUtils.invokeConfirmDialog} method. 
 *
 * @author  Joseph Bowbeer
 * @version 2.0
 */
public class InvokeDialogTest extends JApplet {

    private JLabel jLabel;

    /** Worker. */
    private SwingWorker<Object> worker;

    /** Initializes the form. */
    public InvokeDialogTest() {
        jLabel = new JLabel();
        jLabel.setPreferredSize(new Dimension(200, 100));
        jLabel.setHorizontalAlignment(JLabel.CENTER);
        getContentPane().add(jLabel, BorderLayout.CENTER);
    }

    /** Starts the worker. */
    public synchronized void start() {
        jLabel.setText("Running...");
        worker = new SwingWorker<Object>() {
            /**
             * Simulates a timeout every three seconds and invokes a confirm
             * dialog to see if the user wants to keep going.
             */
            protected Object construct() throws InterruptedException {
                while (true) {
                    Thread.sleep(3000);
                    int n = InvokeUtils.invokeConfirmDialog(
                            jLabel,
                            "Operation timed out. Try again?",
                            "Timeout",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.WARNING_MESSAGE);
                    if (n != JOptionPane.YES_OPTION) {
                        break;
                    }
                }
                return null;
            }
            protected void finished() {
                jLabel.setText("Stopped");
            }
        };
        worker.start();
    }

    /** Stops the worker. */
    public synchronized void stop() {
        if (worker != null) {
            worker.cancel(true);
            worker = null;
        }
    }


    /**
     * Launches this applet in a frame.
     * @param args ignored
     */
    public static void main(String[] args) {
        final JFrame frame = new JFrame("Invoke Dialog Test");
        final JApplet applet = new InvokeDialogTest();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(applet, BorderLayout.CENTER);
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                frame.pack();
                frame.setVisible(true);
                applet.init();
                applet.start();
            }
        });
    }
}
